package com.workplan.client.widgets.employeeitem;

import com.general.client.action.ActionHandler;
import com.general.client.basewidget.BaseWidget;
import com.general.client.modes.WidgetMode;
import com.general.client.widgets.button.blue.BlueButton;
import com.general.client.widgets.button.red.RedButton;
import com.general.client.widgets.combobox.Combobox;
import com.general.client.widgets.textbox.TextBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Widget;
import com.workplan.shared.datamodel.employee.EmployeeDataModel.Role;

public class EmployeeItem extends BaseWidget implements
		EmployeeItemPresenter.Display {

	private static EmployeeItemUiBinder uiBinder = GWT
			.create(EmployeeItemUiBinder.class);

	interface EmployeeItemUiBinder extends UiBinder<Widget, EmployeeItem> {
	}

	@UiField
	TextBox firstName;
	@UiField
	TextBox lastName;
	@UiField
	TextBox phoneNumber;
	@UiField
	TextBox email;
	@UiField
	Combobox<Integer> minShifts;
	@UiField
	Combobox<Integer> maxShifts;
	@UiField
	Combobox<Role> role;

	@UiField
	BlueButton historyButton;
	@UiField
	RedButton removeButton;

	public EmployeeItem() {
		initWidget(uiBinder.createAndBindUi(this));
		initTextItem(firstName, true);
		initTextItem(lastName, true);
		initTextItem(phoneNumber, false);
		initTextItem(email, false);

		initComboboxItem(minShifts, false);
		initComboboxItem(maxShifts, false);
		initComboboxItem(role, true);

		historyButton.getText().setText("History");
		removeButton.getText().setText("Remove");

		for (Integer i = 0; i <= 7; i++) {
			minShifts.addItem(i, i.toString());
		}
		for (Integer i = 0; i <= 7; i++) {
			maxShifts.addItem(i, i.toString());
		}
		role.addItem(Role.EMPLOYEE, "Employee");
		role.addItem(Role.SHIFT_MANAGER, "Shift Manager");
		role.addItem(Role.MANAGER, "Manager");
	}

	private void initTextItem(TextBox item, boolean mandatory) {
		item.setShowLabel(false);
		item.setOnBlurSwitchToView(true);
		item.setOnClickSwitchToEdit(true);
		item.setMandatory(mandatory);
		item.setValidateOnValueChange(true);
		item.setMode(WidgetMode.VIEW);
	}

	private <T> void initComboboxItem(Combobox<T> item, boolean mandatory) {
		item.setShowLabel(false);
		item.setOnBlurSwitchToView(true);
		item.setOnClickSwitchToEdit(true);
		item.setMandatory(mandatory);
		item.setValidateOnValueChange(true);
		item.setMode(WidgetMode.VIEW);
	}

	@Override
	public HandlerRegistration addHistoryActionHandler(ActionHandler handler) {
		return historyButton.addActionHandler(handler);
	}

	@Override
	public HandlerRegistration addRemoveActionHandler(ActionHandler handler) {
		return removeButton.addActionHandler(handler);
	}

	@Override
	public HasText getFirstName() {
		return new HasText() {

			@Override
			public void setText(String text) {
				firstName.setValue(text);
			}

			@Override
			public String getText() {
				return firstName.getValue();
			}
		};
	}

	@Override
	public HandlerRegistration addFirstNameChangeHandler(
			ValueChangeHandler<String> handler) {
		return firstName.addValueChangeHandler(handler);
	}

	@Override
	public HasText getLastName() {
		return new HasText() {

			@Override
			public void setText(String text) {
				lastName.setValue(text);
			}

			@Override
			public String getText() {
				return lastName.getValue();
			}
		};
	}

	@Override
	public HandlerRegistration addLastNameChangeHandler(
			ValueChangeHandler<String> handler) {
		return lastName.addValueChangeHandler(handler);
	}

	@Override
	public HasText getPhoneNum() {
		return new HasText() {

			@Override
			public void setText(String text) {
				phoneNumber.setValue(text);
			}

			@Override
			public String getText() {
				return phoneNumber.getValue();
			}
		};
	}

	@Override
	public HandlerRegistration addPhoneNumChangeHandler(
			ValueChangeHandler<String> handler) {
		return phoneNumber.addValueChangeHandler(handler);
	}

	@Override
	public HasText getEmail() {
		return new HasText() {

			@Override
			public void setText(String text) {
				email.setValue(text);
			}

			@Override
			public String getText() {
				return email.getValue();
			}
		};

	}

	@Override
	public HandlerRegistration addEmailChangeHandler(
			ValueChangeHandler<String> handler) {
		return email.addValueChangeHandler(handler);
	}

	@Override
	public HasValue<Integer> getMinShifts() {
		return new HasValue<Integer>() {

			@Override
			public void fireEvent(GwtEvent<?> event) {
				minShifts.fireEvent(event);
			}

			@Override
			public HandlerRegistration addValueChangeHandler(
					ValueChangeHandler<Integer> handler) {
				return minShifts.addValueChangeHandler(handler);
			}

			@Override
			public void setValue(Integer value, boolean fireEvents) {
				minShifts.setValue(value, fireEvents);
			}

			@Override
			public void setValue(Integer value) {
				minShifts.setValue(value);
			}

			@Override
			public Integer getValue() {
				return minShifts.getValue();
			}
		};
	}

	// this method might be obsolete,
	// as the functionality can be achieved by the
	// getMinShifts().addValueChangeHandler()
	@Override
	public HandlerRegistration addMinShiftsChangeHandler(
			ValueChangeHandler<Integer> handler) {
		return minShifts.addValueChangeHandler(handler);
	}

	@Override
	public HasValue<Integer> getMaxShifts() {
		return new HasValue<Integer>() {

			@Override
			public void fireEvent(GwtEvent<?> event) {
				maxShifts.fireEvent(event);
			}

			@Override
			public HandlerRegistration addValueChangeHandler(
					ValueChangeHandler<Integer> handler) {
				return maxShifts.addValueChangeHandler(handler);
			}

			@Override
			public void setValue(Integer value, boolean fireEvents) {
				maxShifts.setValue(value, fireEvents);
			}

			@Override
			public void setValue(Integer value) {
				maxShifts.setValue(value);
			}

			@Override
			public Integer getValue() {
				return maxShifts.getValue();
			}
		};

	}

	@Override
	public HandlerRegistration addMaxShiftsChangeHandler(
			ValueChangeHandler<Integer> handler) {
		return maxShifts.addValueChangeHandler(handler);
	}

	@Override
	public HasValue<Role> getRole() {
		return new HasValue<Role>() {

			@Override
			public void fireEvent(GwtEvent<?> event) {
				role.fireEvent(event);
			}

			@Override
			public HandlerRegistration addValueChangeHandler(
					ValueChangeHandler<Role> handler) {
				return role.addValueChangeHandler(handler);
			}

			@Override
			public void setValue(Role value, boolean fireEvents) {
				role.setValue(value, fireEvents);
			}

			@Override
			public void setValue(Role value) {
				role.setValue(value);
			}

			@Override
			public Role getValue() {
				return role.getValue();
			}
		};
	}

	@Override
	public HandlerRegistration addRoleChangeHandler(
			ValueChangeHandler<Role> handler) {
		return role.addValueChangeHandler(handler);
	}

}
